<template>
  <div class="header">
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="javascript:;">小米商城</a>
          <a href="javascript:;">Mui</a>
          <a href="javascript:;">loT</a>
          <a href="javascript:;">云服务</a>
          <a href="javascript:;">金融</a>
          <a href="javascript:;">有品</a>
          <a href="javascript:;">小爱开放平台</a>
          <a href="javascript:;">企业团购</a>
          <a href="javascript:;">资质证照</a>
          <a href="javascript:;">协议规则</a>
          <a
            href="javascript:;"
            class="downApp"
            :class="{active:showApp}"
            @mouseover="showApp=true"
            @mouseleave="showApp=false"
          >
            下载app
            <div class="downApp-box">
              <img
                src="//i1.mifile.cn/f/i/17/appdownload/download.png?1"
                alt="小米商城"
                width="90"
                height="90"
              />
              小米商城APP
            </div>
          </a>
          <a href="javascript:;">Select Location</a>
        </div>
        <div class="topbar-user">
          <a href="javascript:;" v-if="username">{{username}}</a>
          <a href="javascript:;" v-if="!username" @click="login">登录</a>
          <router-link :to="{name:'register'}" v-if="!username">注册</router-link>
          <a href="javascript:;">消息通知</a>
          <a href="javascript:;" v-if="username" @click="logout">退出</a>
          <a href="/#/order/list" v-if="username">我的订单</a>
          <div
            class="miniCartTrigger"
            @mouseover.stop="nav_cart_show"
            @mouseleave="showNavCart=false"
          >
            <a href="javascript:;" class="my-cart" :class="{hasCart:cartCount}" @click="goToCart">
              <i class="iconfont" :class="{'icon-cart':!cartCount,'icon-cart-full':cartCount}"></i>
              购物车( {{(cartCount)}} )
            </a>
            <div
              class="nav-cart-list"
              :class="{active:showNavCart}"
              ref="navcartList"
              @mouseenter.stop
            >
              <div class="nav-cart-list-in">
                <loading v-show="loading"></loading>
                <p class="nodata" v-if="!cartCount">购物车中没有商品，快去购买吧</p>
                <ul v-if="cartCount" ref="cartH" class="cart-ul" :class="{ovhy:cartlist.length>3}">
                  <li v-for="(item,index) in cartlist" :key="index" class="nav_cart_item">
                    <div class="cart-item clearfix first">
                      <div class="thumb">
                        <img alt v-lazy="item.productMainImage" />
                      </div>
                      <p class="name">{{item.productName}}</p>
                      <span class="price">{{item.productPrice}}元 × {{item.quantity}}</span>
                      <span class="btn-del J_delItem" @click.stop="delProduct(item)">
                        <b class="iconfont-close">X</b>
                      </span>
                    </div>
                  </li>
                </ul>
              </div>
              <div class="navcart-total" v-if="cartCount">
                <div class="total">
                  <div class="total-count">
                    共
                    <em>{{cartCount}}</em> 件商品
                  </div>

                  <div class="total-price">
                    <em>{{cartTotalPrice}}</em>元
                  </div>
                </div>
                <router-link to="/cart" class="btn btn-primary btn-cart">去购物车结算</router-link>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/#/index"></a>
        </div>
        <div class="header-menu">
          <div class="item-menu">
            <span>小米手机</span>
            <div class="children">
              <ul>
                <li class="product" v-for="(item,index) in phoneList" :key="index">
                  <a :href="'/#/product/'+item.id" target="_blank">
                    <div class="pro-img">
                      <img v-lazy="item.mainImage" :alt="item.subtitle" />
                    </div>
                    <div class="pro-name">{{item.name}}</div>
                    <div class="pro-price">{{item.price || currency}}</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
          <div class="item-menu">
            <span>红米手机</span>
          </div>
          <div class="item-menu">
            <span>电视</span>
            <div class="children">
              <ul>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-1.jpg" />
                    </div>
                    <div class="pro-name">小米壁画电视 65英寸</div>
                    <div class="pro-price">6999元</div>
                  </a>
                </li>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-2.jpg" />
                    </div>
                    <div class="pro-name">小米全面屏电视E55A</div>
                    <div class="pro-price">6999元</div>
                  </a>
                </li>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-3.png" />
                    </div>
                    <div class="pro-name">小米全面屏电视E55A</div>
                    <div class="pro-price">6999元</div>
                  </a>
                </li>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-4.jpg" />
                    </div>
                    <div class="pro-name">小米全面屏电视E55A</div>
                    <div class="pro-price">6999元</div>
                  </a>
                </li>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-5.jpg" />
                    </div>
                    <div class="pro-name">小米全面屏电视E55A</div>
                    <div class="pro-price">6999元</div>
                  </a>
                </li>
                <li class="product">
                  <a href target="_blank">
                    <div class="pro-img">
                      <img src="/imgs/nav-img/nav-3-6.png" />
                    </div>
                    <div class="pro-name">查看全部</div>
                    <div class="pro-price">查看全部</div>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text" name="keyword" />
            <a href="javascript:;"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";

import Loading from "./../components/Loading";
import cartmixin from "./../util/cartmixin.js";
import { debounce } from "./../util/common.js";

export default {
  name: "nav-header",
  mixins: [cartmixin],
  data() {
    return {
      phoneList: [],
      showApp: false,
      showNavCart: false,
      loading: true,
      cartli: 0
      // 购物车商品列表
      // cartlist: [],
      // cartTotalPrice: 0
    };
  },
  components: {
    Loading
  },
  computed: {
    // username(){
    //   let t=this.$store.state.username
    //   window.console.log(t)
    //   return this.$store.state.username;
    // },
    // cartCount(){
    //   return this.$store.state.cartCount;
    // }
    ...mapState(["username", "cartCount"])
  },
  filter: {
    currency(val) {
      if (!val) return "0.00";
      return "￥" + val.toFixed(2) + "元";
    }
  },
  mounted() {
    this.getProductList();
    if (this.$route.params && this.$route.params == "login") {
      window.console.log("登录窗口的登录");
      this.getCartCount();
    }
  },
  methods: {
    // 登录路由跳转：
    login() {
      this.$router.push("/login");
    },
    getProductList() {
      this.axios
        .get("/products", {
          params: {
            categoryId: "100012"
          }
        })
        .then(res => {
          if (res.list.length > 6) {
            this.phoneList = res.list.slice(0, 6);
          }
        });
    },
    getCartCount() {
      // 给定res一个默认值：
      this.axios.get("/carts/products/sum").then((res = 0) => {
        // 截获购物车信息然后保存到VUEX
        this.$store.dispatch("saveCartCount", res);
      });
    },
    // 退出登录：
    logout() {
      this.axios.post("/user/logout").then(() => {
        this.$message.success("退出成功");
        // 清空cookie
        this.$cookie.set("userId", "", { expiress: "-1" });
        // 清空vuex中的登录信息:
        this.$store.dispatch("saveUserName", "");
        // 清空vuex中的购物车信息：
        this.$store.dispatch("saveCartCount", 0);
      });
    },
    // 路由跳转：
    goToCart() {
      this.$router.push("/cart");
    },
    nav_cart_show() {
      if (!this.cartCount) {
        this.loading = false;
        this.showNavCart = true;
        return false;
      }

      this.showNavCart = true;
      this.getCartList();
    },
    // 节流防抖用法：https://www.cnblogs.com/fozero/p/11107606.html

    getCartList: debounce(function() {
      this.axios.get("/carts").then(res => {
        this.renderData(res);
        this.loading = false;
      });
    }, 300),
    renderData(res) {
      this.cartlist = res.cartProductVoList || [];
      this.cartTotalPrice = res.cartTotalPrice;
      // this._getCartHeight();
      console.log(res);
    }
  }
};
</script>

<style lang="less" scoped>
.header {
  .nav-topbar {
    height: 40px;
    line-height: 40px;
    background-color: #333;
    color: #b0b0b0;
    // overflow: hidden;
    .container {
      .flex-nowrap-sb(center);
      a {
        display: inline-block;
        color: #b0b0b0;
        margin: 0 8px;
        .tf3;
        &:hover {
          color: #fff;
        }
      }
      .miniCartTrigger {
        &:hover {
          .my-cart {
            background-color: @colorG;
            color: @colorA;
          }
        }
      }
      .my-cart {
        width: 110px;
        // background-color: #ff6600;
        text-align: center;
        color: #fff;
        margin-right: 0;
        background: #424242;
        height: 40px;
        line-height: 40px;
        // .flex-row-nowrap(center);
        transition: none;
        position: relative;
        &.hasCart {
          background-color: @colorA;
        }

        // &:hover {
        //   background-color: @colorG;
        //   color: @colorA;
        // }
        i {
          margin-right: 6px;
        }
      }
    }
  }
  .nav-header {
    .container {
      position: relative;
      height: 112px;
      .flex-nowrap-sb;
      .header-logo {
        display: inline-block;
        width: 55px;
        height: 55px;
        background-color: #ff6000;
        a {
          display: inline-block;
          width: 110px;
          height: 55px;
          &:before {
            content: "";
            .bgImg(55px, 55px, "/imgs/mi-logo.png", 55px);
            .tf3;
          }
          &:after {
            content: "";
            .bgImg(55px, 55px, "/imgs/mi-home.png", 55px);
          }
          &:hover {
            &:before {
              margin-left: -55px;
            }
          }
        }
      }
      .header-menu {
        display: inline-block;
        width: 643px;
        padding-left: 209px;
        .item-menu {
          display: inline-block;
          color: #333;
          font-weight: bold;
          font-size: 16px;
          line-height: 112px;
          margin-right: 20px;
          span {
            cursor: pointer;
          }
          &:hover {
            color: @colorA;
            .children {
              height: 220px;
              opacity: 1;
            }
          }
          .children {
            position: absolute;
            top: 112px;
            left: 0;
            width: 1226px;
            height: 0;
            opacity: 0;
            border-top: 1px solid #e55555;
            box-shadow: 0px 7px 6px 0px rgba(0, 0, 0, 0.11);
            z-index: 10;
            overflow: hidden;
            transition: all 0.5s;
            background-color: #fff;
            .product {
              float: left;
              width: 16.6%;
              height: 220px;
              font-size: 12px;
              line-height: 12px;
              text-align: center;
              position: relative;
              &:last-child {
                &::before {
                  display: none;
                }
              }
              &:before {
                content: "";
                position: absolute;
                top: 28px;
                right: 0;
                border-left: 1px solid @colorF;
                height: 100px;
              }
              a {
                display: inline-block;
              }
              img {
                height: 111px;
                width: auto;
                margin-top: 26px;
              }
              .pro-img {
                height: 137px;
              }
              .pro-name {
                font-weight: bold;
                margin-top: 19px;
                margin-bottom: 8px;
                color: @colorB;
              }
              .pro-price {
                color: @colorA;
              }
            }
          }
        }
      }
      .header-search {
        width: 319px;
        .wrapper {
          height: 50px;
          border: 1px solid #e0e0e0;
          display: flex;
          align-items: center;
          input {
            box-sizing: border-box;
            border: 1px solid #e0e0e0;
            width: 264px;
            height: 50px;
            padding-left: 14px;
            border-left: none;
          }
          a {
            display: inline-block;
            width: 18px;
            height: 18px;
            background: url("/imgs/icon-search.png") no-repeat center;
            background-size: 18px;
            .bz(contain);
            margin-left: 17px;
          }
        }
      }
    }
  }
}
.downApp {
  position: relative;
  line-height: 40px;
  .downApp-box {
    position: absolute;
    top: 40px;
    left: 50%;
    width: 120px;
    height: 0;
    z-index: 88;
    background: #fff;
    margin-left: -60px;
    -webkit-box-shadow: #aaa 0 1px 5px;
    box-shadow: 0 1px 5px #aaa;
    text-align: center;
    font-size: 14px;
    color: #333;
    line-height: 1;
    overflow: hidden;
    -webkit-transition: height 0.3s;
    transition: height 0.3s;
    img {
      display: block;
      width: 90px;
      height: 90px;
      margin: 18px auto 12px;
    }
  }
  &.active {
    &::before {
      content: "";
      position: absolute;
      bottom: 0;
      left: 50%;
      width: 0;
      height: 0;
      margin-left: -8px;
      border-width: 0 8px 8px;
      border-style: solid;
      border-color: rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) #fff;
    }
    .downApp-box {
      height: 148px;
    }
  }
}
.topbar-user {
  .flex-row-nowrap(center);
  > span {
    position: relative;
    bottom: 1px;
  }
}
.nav-cart-list .loading {
  height: 25px;
  line-height: 25px;
  margin: 0 20px 20px;
}
.nav-cart-list-in {
  width: 100%;
  height: 100%;
  padding: 20px 20px 0 20px;
}
.cart-item {
  .flex-nowrap-sb(center);
}
.cart-item .thumb {
  width: 60px;
  height: 60px;
}
.cart-item .thumb img {
  width: 60px;
  height: 60px;
}
.cart-item .name {
  width: 95px;
  // height: 40px;
  line-height: 20px;
  margin: 10px 0;
  color: #424242;
  overflow: hidden;
}
.nav_cart_item {
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #ccc;
  &:last-child {
    border-bottom: none;
  }
}
.navcart-total {
  padding: 15px 20px;
  background: #fafafa;
  .flex-nowrap-sb;
  .total {
    float: left;
    line-height: 20px;
  }
  .total-count {
    text-align: left;
    color: #757575;
    em {
      font-style: normal;
    }
  }
  .total-price {
    display: block;
    font-weight: 400;
    color: #ff6700;
    text-align: left;
    margin-top: 8px;
    em {
      font-size: 24px;
      font-style: normal;
    }
  }
}
.header .nav-topbar .container .navcart-total .btn-cart {
  font-size: 14px;
  line-height: 40px;
  height: 40px;
  .cf;
  .tf3;
  color: 1px solid transparent;
  &:hover {
    background-color: #fff;
    color: @colorA;
    border: 1px solid @colorA;
  }
  a {
    color: #fff !important;
  }
}
.header .nav-topbar .container .J_delItem {
  opacity: 0;
  .tf2;
  cursor: pointer;
}
.header .nav-topbar .container .nav_cart_item:hover .J_delItem {
  opacity: 1;
}
.nav-cart-list {
  position: absolute;
  right: 0;
  top: 40px;
  z-index: 555;
  width: 320px;
  color: #424242;
  padding: 0px 0 0 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  background-color: #fff;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.5s ease;
  &.active {
    max-height: 600px;
    opacity: 1;
  }
  .nodata {
    .c3;
    padding-bottom: 20px;
  }
}
.topbar-user {
  position: relative;
}
.cart-ul {
  &.ovhy {
    max-height: 284px;
    overflow: hidden scroll;
  }
}
</style>